home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / LocateRegistry.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  138 lines

  1. /*
  2.  * @(#)LocateRegistry.java    1.8 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.rmi.registry;
  16.  
  17. import java.rmi.RemoteException;
  18. import java.rmi.UnknownHostException;
  19.  
  20. /**
  21.  * This class is used to obtain the bootstrap Registry on a particular
  22.  * host (including your local host). The following example demonstrates usage
  23.  * (minus exception handling):
  24.  *
  25.  * <br> Server wishes to make itself available to others:
  26.  *    <br> SomeService service = ...; // remote object for service
  27.  *    <br> Registry registry = LocateRegistry.getRegistry();
  28.  *    <br> registry.bind("I Serve", service);
  29.  *
  30.  * <br> The client wishes to make requests of the above service:
  31.  *    <br> Registry registry = LocateRegistry.getRegistry("foo.services.com");
  32.  *    <br> SomeService service = (SomeService)registry.lookup("I Serve");
  33.  *    <br> service.requestService(...);
  34.  *
  35.  * @see Registry
  36.  */
  37. public final class LocateRegistry {
  38.  
  39.     /**
  40.      * Find registry package prefix: assumes that the implementation class
  41.      * RegistryHandler is located in the package defined by the prefix.
  42.      */
  43.     private static String registryPkgPrefix =
  44.         System.getProperty("java.rmi.registry.packagePrefix",
  45.                "sun.rmi.registry");
  46.               
  47.     private static RegistryHandler handler = null;
  48.     
  49.     /*
  50.      * Private constructor to disable public construction.
  51.      */
  52.     private LocateRegistry() {}
  53.  
  54.     /**
  55.      * Returns the remote object Registry for the local host.
  56.      */
  57.     public static Registry getRegistry()
  58.     throws RemoteException
  59.     {
  60.     try {
  61.         return getRegistry(null, Registry.REGISTRY_PORT);
  62.     } catch (UnknownHostException ex) {
  63.         // Can't happen
  64.     }
  65.     return null;
  66.     }
  67.  
  68.     /**
  69.      * Returns the remote object Registry on the current host at the
  70.      * specified port.
  71.      */
  72.     public static Registry getRegistry(int port)
  73.     throws RemoteException
  74.     {
  75.     try {
  76.         return getRegistry(null, port);
  77.     } catch (UnknownHostException ex) {
  78.         // Can't happen
  79.     }
  80.     return null;
  81.     }
  82.     
  83.     /**
  84.      * Returns the remote object Registry on the specified host at a
  85.      * default (i.e., well-known) port number.  If the host
  86.      * <code>String</code> reference is <code>null</code>, the local
  87.      * host is used.
  88.      */
  89.     public static Registry getRegistry(String host)
  90.     throws RemoteException, UnknownHostException
  91.     {
  92.     return getRegistry(host, Registry.REGISTRY_PORT);
  93.     }
  94.     
  95.     /**
  96.      * Returns the remote object Registry on the specified host at the
  97.      * specified port.  If port <= 0, the default Registry port number
  98.      * is used.  If the host <code>String</code> reference is
  99.      * <code>null</code>, the local host is used.
  100.      *
  101.      * @exception UnknownHostException If the host is not known.
  102.      */
  103.     public static Registry getRegistry(String host, int port)
  104.     throws RemoteException, UnknownHostException
  105.     {
  106.     if (handler != null) {
  107.         return handler.registryStub(host, port);
  108.     } else {
  109.         throw new RemoteException("No registry handler present");
  110.     }
  111.     
  112.     }
  113.  
  114.     /**
  115.      * Create and export a registry on the local host.
  116.      *
  117.      * @param port the port on which the registry is to be exported
  118.      * @exception RemoteException If failure occurs during remote
  119.      * object creation.
  120.      */
  121.     public static Registry createRegistry(int port) throws RemoteException
  122.     {
  123.     if (handler != null) {
  124.         return handler.registryImpl(port);
  125.     } else {
  126.         throw new RemoteException("No registry handler present");
  127.     }
  128.     }
  129.  
  130.     static {
  131.     try {
  132.         Class cl = Class.forName(registryPkgPrefix + ".RegistryHandler");
  133.         handler = (RegistryHandler)(cl.newInstance());
  134.     } catch (Exception e) {
  135.     }
  136.     }
  137. }
  138.